package com.mimiwang.chat.service.Imp;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mimiwang.chat.config.TalkPageConfiguration;
import com.mimiwang.chat.dao.TalkDao;
import com.mimiwang.chat.entity.Talk;
import com.mimiwang.chat.entity.TalkVo;
import com.mimiwang.chat.service.ChatTalkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @AUTHOR:游李滨
 * @DATE:2022/10/6 21:35
 */

@Service
public class ChatTalkServiceImp implements ChatTalkService {

    @Autowired
    TalkDao talkDao;

    @Autowired
    TalkPageConfiguration talkPageConfiguration;


    @Autowired
    RedisTemplate<String,Object> redisTemplate;




    @Override
    public int insertOne(Talk talk) {
        if(talk!=null){
            System.out.println(talk);
            return talkDao.insertOne(talk);
        }
        return 0;
    }



    /**
     *  talk装饰器，用于talk转换为talkVo
     *
     * */

    public TalkVo trim(Talk talk) {

        return new TalkVo(talk);
    }



    @Override
    public PageInfo<TalkVo> selectAll(long sender, long receiver,int pageNum)
    {
        //分页设置
        PageHelper.startPage(pageNum,talkPageConfiguration.getPageSize());

        //查询所有对话
        List<Talk> talks = talkDao.selectAll(sender, receiver);

//        处理对话数组，将其转换成talkVo数组
        List<TalkVo> talkVoList=new ArrayList<>();
        for(int i=talks.size()-1;i>=0;i--){
            Talk talk = talks.get(i);
            TalkVo trim = this.trim(talk);
//            判断该条消息是否是sender发送的
            trim.setMine(talk.getSender()==sender);
            talkVoList.add(trim);
        }

        return new PageInfo<>(talkVoList);

    }


}
